import JsonP from 'jsonp'
import axios from 'axios';
import {Modal} from 'antd';
export default class Axios {
    // jsonp方法封装
    static jsonp(options) {
        return new Promise((resolve, reject) => {
            JsonP(options.url, {
                param: 'callback'
            }, (err, response) => {
                if (response.status === 'success') {
                    resolve(response)
                } else {
                    reject(response)
                }
            })
        })
    }

    // ajax方法封装
    static Ajax(options) {
        let loading;
        if (options.data && options.data.isShowLoading !== false) {
            loading = document.getElementById('ajaxLoading');
            loading.style.display = 'block'
        }
        let baseApi = 'https://www.easy-mock.com/mock/5b8c9810c0b06d42b3bb5552/mockapi'
        return new Promise((resolve, reject) => {
            axios({
                url: options.url,
                method: options.method,
                baseURL: baseApi,
                timeout: 10000,
                params:(options.data && options.data.params) || ''
            }).then((response)=> {
                if (options.data && options.data.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading');
                    loading.style.display = 'none'
                }
                if (response.status === 200) {
                    let res = response.data;
                    if (res.code === 0) {
                        resolve(response.data)
                    } else {
                        Modal.info({
                            title: '提示',
                            content: res.msg
                        })
                    }
                } else {
                    reject(response)
                }
            })
        })
    }
}